import json
from django.template.response import TemplateResponse
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
import asc_db.forms as forms
import asc_db.models as models

#------------------------------------------------------------------------------


def home_page(request):
    return TemplateResponse(request, 'home.html', {})

#------------------------------------------------------------------------------


def search_page(request, active_tab=None):
    # Check that the active tab variable is a valid tab
    if active_tab not in ['person', 'address', 'group_or_team']:
        active_tab = None
    return TemplateResponse(request, 'search.html', {
                                                     "active_tab": active_tab
                                                    })


#------------------------------------------------------------------------------


def add_page(request, active_tab=None):
    # Check that the active tab variable is a valid tab
    if active_tab not in ['person', 'address', 'group_or_team']:
        active_tab = None

    return TemplateResponse(request, 'add.html', {
                                                  "active_tab": active_tab
                                                 })

#------------------------------------------------------------------------------


def reports_page(request):
    return TemplateResponse(request, 'reports.html', {})

#------------------------------------------------------------------------------


def group_or_team_form(request, group_id=None):
    instance = models.GroupOrTeam.objects.get_unique_or_none(pk=group_id)

    if request.method == 'POST':  # If the form has been submitted...
        form = forms.GroupOrTeamForm(request.POST, instance=instance)
        if form.is_valid():
            group_or_team = form.save()
            messages.add_message(request,
                                 messages.SUCCESS,
                                'Successfully added group %s' % group_or_team)
            return HttpResponseRedirect(reverse('group_or_team_form'))
        else:
            messages.add_message(request,
                                 messages.ERROR,
                                 'Could not add group/team')
    else:
        form = forms.GroupOrTeamForm(instance=instance)

    return TemplateResponse(request, 'forms/groups_form.html', {
                                                                'form': form,
                                                               })

#------------------------------------------------------------------------------


def address_form(request, address_id=None):
    instance = models.Address.objects.get_unique_or_none(pk=address_id)

    if request.method == 'POST':  # If the form has been submitted...
        form = forms.AddressForm(request.POST, instance=instance)
        if form.is_valid():
            address = form.save()
            messages.add_message(request,
                                 messages.SUCCESS,
                                'Successfully added address %s' % address)
            return HttpResponseRedirect(reverse('address_form'))
        else:
            messages.add_message(request,
                                 messages.ERROR,
                                 'Could not add address')
    else:
        form = forms.AddressForm(instance=instance)

    return TemplateResponse(request,
                            'forms/address_form.html',
                            {
                             'form': form,
                            })

#------------------------------------------------------------------------------


def person_form(request, person_id=None):
    instance = models.Person.objects.get_unique_or_none(pk=person_id)

    if request.method == 'POST':  # If the form has been submitted...
        form = forms.PersonForm(request.POST, instance=instance)
        if form.is_valid():
            person = form.save()
            messages.add_message(request,
                                 messages.SUCCESS,
                                'Successfully added person %s' % person)
            return HttpResponseRedirect(reverse('person_form'))
        else:
            messages.add_message(request,
                                 messages.ERROR,
                                 'Could not add member')
    else:
        form = forms.PersonForm(instance=instance)
    return TemplateResponse(request, 
                            'forms/person_form.html',
                            {
                             'form': form,
                            })
